package com.csair.seam.controller.pc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.csair.seam.domain.model.entity.IdleReceiveExecl;
import com.csair.seam.domain.model.param.IdleReceiveParam;
import com.csair.seam.domain.service.IdleReceiveService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csair.seam.domain.model.entity.Equip;
import com.csair.seam.domain.model.entity.ShareEquip;
import com.csair.seam.domain.model.entity.UserBehavior;
import com.csair.seam.domain.model.enums.UserBehaviorType;
import com.csair.seam.domain.model.enums.VisitsModule;
import com.csair.seam.domain.model.param.ShareEquipParam;
import com.csair.seam.domain.model.vo.ShareEquipModuleVo;
import com.csair.seam.domain.service.IEquipService;
import com.csair.seam.domain.service.ISharingCenterService;
import com.csair.seam.infrastructure.log.VisitsLog;
import com.csair.seam.infrastructure.web.BaseController;
import com.csair.seam.infrastructure.web.BaseResponse;
import com.csair.seam.infrastructure.web.ResponseCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "共享中心")
@RestController
@RequestMapping("/pc/sharing")
public class SharingCenterPcController extends BaseController{
	
	@Autowired
	@Qualifier("sharingCenterService")
	private ISharingCenterService sharingCenterService;

	@Autowired
	private IdleReceiveService idleReceiveService;

	@Autowired
	private IEquipService equipService;
	
	private List<UserBehavior> getMyFollowBehaviorList() {
		UserBehavior userBehavior = new UserBehavior();
		userBehavior.setUserCode(getAccount());
		userBehavior.setBehaviorType(UserBehaviorType.FOLLOW.getName());
		List<UserBehavior> myFollowBehaviorList = sharingCenterService.getUserBehaviorList(userBehavior);
		if(CollectionUtils.isEmpty(myFollowBehaviorList)) {
			return Collections.emptyList();
		}
		return myFollowBehaviorList;
	}

	@GetMapping("/getIdleReceive")
	@ApiOperation(value = "模块展示设备列表", notes = "getCategoryEquipList",httpMethod="GET")
	public Object  getIdleReceive(IdleReceiveParam param){
		List<IdleReceiveExecl> execl = idleReceiveService.getIdleReceive(param);
		List<IdleReceiveExecl> returnExecl = new ArrayList<>();
		execl.stream().forEach(e->{
			returnExecl.add(new IdleReceiveExecl(e.getIncorp(),e.getReceivnum(),e.getCategory()));
		});
		return buildSuccess(returnExecl);
	}
	
	@GetMapping("/categoryEquipList")
    @ApiOperation(value = "模块展示设备列表", notes = "getCategoryEquipList",httpMethod="GET")
//	@VisitsLog(module = VisitsModule.SHARING_CENTER)
	@VisitsLog(module = VisitsModule.SHARING_CENTER_QUERY)
	public BaseResponse<List<ShareEquipModuleVo>> getCategoryEquipList(@ApiParam ShareEquipParam param) {
		List<ShareEquipModuleVo> moduleShareEquipList = sharingCenterService.findModuleShareEquipListForPc(param);
		List<UserBehavior> myFollowBehaviorList = getMyFollowBehaviorList();
		for (ShareEquipModuleVo shareEquipModuleVo : moduleShareEquipList) {
			for (ShareEquip shareEquip : shareEquipModuleVo.getList()) {
				if(myFollowBehaviorList.stream().anyMatch(e->e.getEquipCode().equals(shareEquip.getCode()))) {
					shareEquip.setFollow("Y");
				}else {
					shareEquip.setFollow("N");
				}
			}
		}
		return buildSuccess(moduleShareEquipList);
	}
	
	@GetMapping("/more")
    @ApiOperation(value = "查看更多", notes = "getMoreEquipList",httpMethod="GET")
	public BaseResponse<List<ShareEquip>> getMoreEquipList(@ApiParam ShareEquipParam param) {
		if(param.getCategoryId() == null) {
			return new BaseResponse<>(ResponseCode.PARAM_ERROR,"参数错误");
		}
		List<ShareEquip> shareEquipList = sharingCenterService.findSharingEquipByParam(param);
		List<UserBehavior> myFollowBehaviorList = getMyFollowBehaviorList();
		for (ShareEquip shareEquip : shareEquipList) {
			if(myFollowBehaviorList.stream().anyMatch(e->e.getEquipCode().equals(shareEquip.getCode()))) {
				shareEquip.setFollow("Y");
			}else {
				shareEquip.setFollow("N");
			}
		}
		return buildSuccess(shareEquipList);
	}
	
	
	@GetMapping("/detail/{equipCode}")
    @ApiOperation(value = "浏览设备详情", notes = "getEquipByCode",httpMethod="GET")
    public BaseResponse<ShareEquip> getEquipByCode(@PathVariable @ApiParam(value="设备编码",required = true) String equipCode) {
    	ShareEquip shareEquip = sharingCenterService.findShareEquipDetail(equipCode);
    	if(shareEquip == null) {
    		return new BaseResponse<>(ResponseCode.PARAM_ERROR,"没有该设备",null);
    	}
    	
    	UserBehavior userBehavior = new UserBehavior();
    	userBehavior.setUserCode(getAccount());
    	userBehavior.setEquipCode(equipCode);
    	userBehavior.setBehaviorType(UserBehaviorType.FOLLOW.getName());
    	List<UserBehavior> userBehaviorList = sharingCenterService.getUserBehaviorList(userBehavior);
    	shareEquip.setFollow(CollectionUtils.isEmpty(userBehaviorList)?"N":"Y");
    	
    	Equip equip = equipService.findEquipByCode(shareEquip.getCode());
    	shareEquip.setEquip(equip);
    	return buildSuccess(shareEquip);
    }
	
	@GetMapping("/myfollows")
	@ApiOperation(value = "查询我的关注", notes = "getMyFollows",httpMethod="GET")
	@VisitsLog(module = VisitsModule.SHARING_CENTER_MYFOLLOW)
	public BaseResponse<IPage<ShareEquip>> getMyFollows(ShareEquipParam param) {
		List<UserBehavior> myFollowBehaviorList = getMyFollowBehaviorList();
		Page<ShareEquip> shareEquipPage = new Page<ShareEquip>();
		if(myFollowBehaviorList.isEmpty()) {
			return buildSuccess(shareEquipPage);
		}
		List<String> equipCodeList = myFollowBehaviorList.stream().map(UserBehavior::getEquipCode).collect(Collectors.toList());
		param.setEquipCodeList(equipCodeList);
		shareEquipPage = sharingCenterService.findSharingEquipPage(param);
		for (ShareEquip shareEquip : shareEquipPage.getRecords()) {
			shareEquip.setFollow("Y");
		}
		return buildSuccess(shareEquipPage);
		
	}
	
	@GetMapping("/follow/{equipCode}")
	@ApiOperation(value = "关注设备", notes = "followEquip",httpMethod="GET")
	public BaseResponse<Integer> followEquip(@PathVariable @ApiParam(value="设备编码",required = true) String equipCode) {
		UserBehavior userBehavior = new UserBehavior();
		userBehavior.setUserCode(getAccount());
		userBehavior.setEquipCode(equipCode);
		userBehavior.setBehaviorType(UserBehaviorType.FOLLOW.getName());
		return buildSuccess(sharingCenterService.addOrUpdateUserBehavior(userBehavior));
	}
	
	@GetMapping("/cancelFollow/{equipCode}")
	@ApiOperation(value = "取消关注", notes = "cancelFollow",httpMethod="GET")
	public BaseResponse<Integer> cancelFollow(@PathVariable @ApiParam(value="设备编码",required = true) String equipCode) {
		UserBehavior userBehavior = new UserBehavior();
		userBehavior.setUserCode(getAccount());
		userBehavior.setEquipCode(equipCode);
		userBehavior.setBehaviorType(UserBehaviorType.FOLLOW.getName());
		return buildSuccess(sharingCenterService.delUserBehavior(userBehavior));
	}
	
	@PostMapping("/search")
	@ApiOperation(value = "搜索设备", notes = "searchEquip",httpMethod="POST")
	public BaseResponse<List<ShareEquip>> searchEquip(ShareEquipParam param) {
		List<ShareEquip> shareEquipList = sharingCenterService.findSharingEquipByParam(param);
		return buildSuccess(shareEquipList);
	}
	
	@GetMapping("/thumb/{equipCode}")
    @ApiOperation(value = "获取设备缩略图", notes = "getEquipThumbByCode",httpMethod="GET")
    public BaseResponse<byte[]> getEquipThumbByCode(@PathVariable @ApiParam(value="设备编码",required = true) String equipCode) {
    	ShareEquip equip = sharingCenterService.findShareEquipThumb(equipCode);
    	if(equip == null) {
    		return buildSuccess(null); 
    	}
    	return buildSuccess(equip.getPreViewImage()); 
    }
	
}
